home *** CD-ROM | disk | FTP | other *** search
- Path: wkaufman.us.oracle.com!wkaufman
- From: wkaufman@wkaufman.us.oracle.com (William Kaufman)
- Newsgroups: comp.lang.c
- Subject: Re: Problem Negating an Unsigned Char
- Date: 3 Mar 1996 17:54:10 GMT
- Organization: Oracle Corporation, Redwood Shores CA
- Message-ID: <4hcmc2$96l@inet-nntp-gw-1.us.oracle.com>
- References: <Dnnros.Lq.0.-s@hkusuc.hku.hk>
- NNTP-Posting-Host: wkaufman.us.oracle.com
-
- In article <Dnnros.Lq.0.-s@hkusuc.hku.hk> h8716718@hkusua.hku.hk (Starry Hung) writes:
- ]
- ] unsigned char a=0x11;
- ] unsigned char b=0xEE;
- ] int c=0;
- ]
- ] void main( void ) {
- ] if( a == ~b ) {
- ] c=1;
- ] }
- ] }
- ]
- ] The c remains unchange, while it changes to 1 if I cast the ~b to unsigned
- ] char as if( a == (unsigned char) ~b )
-
- I _think_ (check me, folks!) that "~" is one of the operators that
- promotes its operand to int. So,
-
- ~(int)((unsigned char)0xEE) == 0xFF(...for sizeof(int) bytes...)11
-
- To match this type, "a" is also promoted to int, and, since it's
- less than UCHAR_MAX/2, there's no sign-extension--it's just 0x00..11.
- So the two don't compare equal.
-
- I guess you could work around it by declaring the variables as
- unsigned int instead,....
-
- -- Bill K.
-
- Bill Kaufman | "I mean ... it's not even been a two-and-two-make-
- wkaufman@us.oracle.com | five sort of day, it's more like a two-and-two-
- | make ... *fish* ..." -- "Cages", Dave McKean
-